package com.yami.shop.mystery.api.controller;

import cn.hutool.core.util.ObjectUtil;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.mystery.common.dto.CompoundDto;
import com.yami.shop.mystery.common.dto.ProdCompoundDto;
import com.yami.shop.mystery.common.service.CompoundRecordServer;
import com.yami.shop.mystery.common.service.CompoundServer;
import com.yami.shop.mystery.common.vo.CompoundProdVo;
import com.yami.shop.mystery.common.vo.CompoundRecordVo;
import com.yami.shop.mystery.common.vo.CompoundVo;
import com.yami.shop.security.common.bo.UserInfoInTokenBO;
import com.yami.shop.security.common.util.AuthUserContext;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 商品合成Controller
 */
@RestController
@RequestMapping("/platform/app")
public class CompoundControllerApp {

    @Autowired
    private CompoundServer spuCompoundServer;
    @Autowired
    private CompoundRecordServer compoundRecordServer;

    @GetMapping("/ua/getSpuList")
    @Operation(summary = "获取合成列表")
    public ServerResponseEntity<PageVO<CompoundVo>> getSpuList(@Valid PageDTO pageDTO, ProdCompoundDto spuCompoundDTO) {
        return ServerResponseEntity.success(spuCompoundServer.getAppSpuList(pageDTO, spuCompoundDTO));
    }


    @PostMapping("/getCompoundSpu")
    @Operation(summary = "商品合成")
    public ServerResponseEntity<CompoundProdVo> getCompoundSpu(@RequestBody CompoundDto compoundDto) {
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        // 检查合成材料
        for (String token :compoundDto.getPresents()){
            String s = RedisUtil.STRING_REDIS_TEMPLATE.opsForValue().get(userInfoInTokenBO.getUserId()+"compound_" + token);
            if (ObjectUtil.isNotNull(s)) {
                return ServerResponseEntity.showFailMsg("材料已被使用");
            }
        }
        try {
            compoundDto.getPresents().forEach(t -> RedisUtil.STRING_REDIS_TEMPLATE.opsForValue().set(userInfoInTokenBO.getUserId()+"compound_" + t, t));
//            CompoundProdVO compoundSpu = spuCompoundServer.getCompoundSpu(compoundDto.getCompoundId(), compoundDto.getPresents());
            return ServerResponseEntity.success();
        } catch (Exception e) {
            // 异常解锁合成材料
            compoundDto.getPresents().forEach(t -> {
                RedisUtil.del(userInfoInTokenBO.getUserId()+"compound_" + t);
            });
            return ServerResponseEntity.showFailMsg(e.getMessage());
        }
    }

    @GetMapping("/getCompoundRecordPage")
    @Operation(summary = "获取合成列表")
    public ServerResponseEntity<PageVO<CompoundRecordVo>> getCompoundRecordPage(@Valid PageDTO pageDTO) {
        return ServerResponseEntity.success(compoundRecordServer.getCompoundPage(pageDTO));
    }

    @GetMapping("/getCompoundRecordDetail")
    @Operation(summary = "获取合成详情")
    public ServerResponseEntity<CompoundRecordVo> getCompoundRecordDetail(int compoundRecordId) {
        return ServerResponseEntity.success(compoundRecordServer.getCompoundRecordDetail(compoundRecordId));
    }

}
